#pragma once

//#include "p2p_session.h"
#include <cstdint>
#include <map>
#include <mutex>
#include <memory>
#include <thread>
#include <vector>
#include <list>
#include "EventLoopMgr.h"
extern "C" {
#include "mongoose.h"
}

class P2PSession
{
public:
	uint16_t m_uId = -1;
	std::string m_sName;

};


class P2PServer
{
public:
	P2PServer& operator=(const P2PServer&) = delete;
	P2PServer(const P2PServer&) = delete;
	P2PServer();
	virtual ~P2PServer();

	//void SetVideoSource(rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> video_source);

	bool Start(uint16_t port, std::string rootPath, std::string ip = "localhost");
	void Stop();

	bool IsExistence(uint16_t accountNum);
	bool DelAccount(uint16_t accountNum);
	int GetListOnLineArray(mg_connection* con, std::vector<uint16_t>& vec);

	void DoWsMeg(struct mg_connection* conn, void* ev_data, void* user_data);

	bool TimerFun();
private:
	static void OnEvent(struct mg_connection* conn, int ev, void* ev_data);

	void HandleHttpEvent(struct mg_connection* c, void* ev_data, void* user_data);

	struct mg_mgr mgr_;
	struct mg_connection* connection_ = nullptr;
	std::unique_ptr<std::thread> poll_thread_;

	std::mutex mutex_;
	//std::map<mg_connection*, rtc::scoped_refptr<P2PSession>> p2p_sessios_;
	std::map<mg_connection*, std::unique_ptr<P2PSession>> p2p_sessios_;

	int m_count = 0;
	std::shared_ptr<TaskScheduler> m_TaskScheduler;
	int64_t			m_nTimerId = -1;

	std::string m_root_dir = "./";

	//rtc::scoped_refptr<webrtc::VideoTrackSourceInterface>   video_source_;
	//rtc::scoped_refptr<webrtc::AudioSourceInterface>        audio_source_;

	//std::unique_ptr<rtc::Thread> signaling_thread_;
	//rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory_;
public:
	static std::list<std::string> m_listLoge;
	static std::string GetLocalIPAddress();
};